package com.handpay.zztong.hp.other.activity;

import java.lang.ref.WeakReference;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.handpay.framework.utils.BitmapLoader;
import com.handpay.framework.utils.BitmapLoader.ImageCallback;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;

/**
 * 刷卡器大图页面
 * 
 * @author cqli
 * 
 */
public class FullScreenImageActivty extends ZZTongActivity
{
	/**
	 * bitmap类型图片
	 */
	public static String	PIC_BITMAP	= "picBitmap";
	public static String	KEY_RES_ID	= "res_id";
	public static String	KEY_PIC_URI	= "pic_uri";
	
	private static class MyImageCallback implements ImageCallback
	{
		WeakReference<ImageView>	mImageViewReferece;
		
		public MyImageCallback(ImageView iv)
		{
			mImageViewReferece = new WeakReference<ImageView>(iv);
		}
		
		@Override
		public void imageLoaded(final Bitmap image, String imageUrl)
		{
			ImageView iv = mImageViewReferece.get();
			if (null != iv)
			{
				if (null != image)
				{
					Bitmap b = CommonUtils.zoomImg(image, ZZTongActivity.screenWidth);
					iv.setImageBitmap(b);
				}
				else
				{
					iv.setImageBitmap(null);
				}
			}
		}
	}
	
	/**
	 * 地址类型图片
	 */
	public static String	PIC	= "pic";
	private Bitmap			bitmap;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_full_screen_image);
		// 按照应用内资源、拍照结果uri、服务器上图片、bitmap依次查找图片并显示
		int localRes = getIntent().getIntExtra(KEY_RES_ID, -1);
		// bm可能是uri也可能是服务器地址。
		String bm = getIntent().getStringExtra(KEY_PIC_URI);
		byte[] bmpArray = getIntent().getByteArrayExtra(PIC_BITMAP);
		ImageView iv = (ImageView) this.findViewById(R.id.iv_photo);
		if (-1 != localRes)
		{
			iv.setImageResource(localRes);
		}
		else if (bm != null)
		{
			MyImageCallback t = new MyImageCallback(iv);
			BitmapLoader.getInstance(getApplicationContext()).asynLoadBitmap(
					bm, bm, t);
		}
		else if (bmpArray != null)
		{
			bitmap = BitmapFactory
					.decodeByteArray(bmpArray, 0, bmpArray.length);
			iv.setImageBitmap(bitmap);
		}
		// iv.getLayoutParams().height = ZZTongActivity.screenHeight / 2;
		iv = (ImageView) this.findViewById(R.id.iv_close);
		close(iv);
	}
	
	private void close(View view)
	{
		view.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				finishView();
			}
		});
	}
	
	@Override
	protected void goBack()
	{
		super.goBack();
		finishView();
	}
	
	private void finishView()
	{
		finish();
		if (bitmap != null)
		{
			bitmap.recycle();
			bitmap = null;
		}
	}
	
	@Override
	protected boolean isShownTopBar()
	{
		return false;
	}
	
	@Override
	protected boolean isShownTabs()
	{
		return false;
	}
}
